﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class RuleGroupDAL
    {
        private static RuleGroup CreateRuleGroupFromReader(SqlDataReader reader)
        {
            RuleGroup ruleGroup = new RuleGroup
            {
                RuleID = Utility.StringParse(reader[RuleGroupColumns.RuleID.ToString()]),
                GroupID = Utility.StringParse(reader[RuleGroupColumns.GroupID.ToString()]),
                Type = Utility.StringParse(reader[RuleGroupColumns.Type.ToString()]),
                Day = Utility.IntParse(reader[RuleGroupColumns.Day.ToString()]),
                FirstDayAmount = Utility.DecimalParse(reader[RuleGroupColumns.FirstDayAmount.ToString()]),
                Value = Utility.DecimalParse(reader[RuleGroupColumns.Value.ToString()]),
            };

            ruleGroup.Rule = new Entities.Rule
            {
                RuleID = ruleGroup.RuleID,
                RuleName = Utility.StringParse(reader[RuleColumns.RuleName.ToString()]),
            };

            ruleGroup.Group = new Group
            {
                GroupID = ruleGroup.GroupID,
                Order = Utility.IntParse(reader[GroupColumns.Order.ToString()]),
            };

            return ruleGroup;
        }

        public static List<RuleGroup> GetRuleGroupList(string ruleID, string type, string orderBy, string direction)
        {
            try
            {
                List<RuleGroup> ruleGroupList = new List<RuleGroup>();

                DataAccessHelper.SqlCmd.Parameters.Add(RuleGroupColumns.RuleID.ToString(), SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("Type", SqlDbType.NVarChar).Value = type;
                DataAccessHelper.SqlCmd.Parameters.Add("OrderBy", SqlDbType.NVarChar).Value = orderBy;
                DataAccessHelper.SqlCmd.Parameters.Add("Direction", SqlDbType.NVarChar).Value = direction;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleGroupGetList))
                {
                    while (reader.Read())
                    {
                        ruleGroupList.Add(CreateRuleGroupFromReader(reader));
                    }
                }

                return ruleGroupList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleGroupList", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEGROUP_GETLIST);
            }
        }

        public bool InsertRuleGroup(RuleGroup ruleGroup)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleGroup.RuleID;
                DataAccessHelper.SqlCmd.Parameters.Add("GroupID", SqlDbType.NVarChar).Value = ruleGroup.GroupID;
                DataAccessHelper.SqlCmd.Parameters.Add("Type", SqlDbType.NVarChar).Value = ruleGroup.Type;
                DataAccessHelper.SqlCmd.Parameters.Add("Day", SqlDbType.Int).Value = ruleGroup.Day;
                DataAccessHelper.SqlCmd.Parameters.Add("FirstDayAmount", SqlDbType.Decimal).Value = ruleGroup.FirstDayAmount;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleGroupInsert) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RULEGROUP_INSERT);
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertRuleGroup", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEGROUP_INSERT);
            }
        }

        public bool UpdateRuleGroup(RuleGroup ruleGroup)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleGroup.RuleID;
                DataAccessHelper.SqlCmd.Parameters.Add("GroupID", SqlDbType.NVarChar).Value = ruleGroup.GroupID;
                DataAccessHelper.SqlCmd.Parameters.Add("Type", SqlDbType.NVarChar).Value = ruleGroup.Type;
                DataAccessHelper.SqlCmd.Parameters.Add("Day", SqlDbType.Int).Value = ruleGroup.Day;
                DataAccessHelper.SqlCmd.Parameters.Add("FirstDayAmount", SqlDbType.Decimal).Value = ruleGroup.FirstDayAmount;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleGroupUpdate) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RULEGROUP_UPDATE);
            }
            catch (SqlException ex)
            {
                Log.LogEx("UpdateRuleGroup", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEGROUP_UPDATE);
            }
        }
    }
}